var app = getApp(), WxEmoji = require("../../libs/WxEmojiView/WxEmojiView.js"), Toast = require("../../libs/zanui/toast/toast");

Page({
    data: {
        showMsg: !1,
        adjust: !0,
        bottom: 0,
        showConfirm: !1,
        pages: 1,
        hide: !0,
        more: !0,
        refresh: !0
    },
    onLoad: function(t) {
        var e = this;
        app.viewCount();
        var a = wx.getStorageSync("userInfo");
        e.setData({
            itemid: t.itemid,
            fromuid: t.fromuid,
            touid: a.memberInfo.uid
        }), e.initChat(a), e.getChatMsg(), WxEmoji.bindThis(e);
    },
    getChatMsg: function() {
        var i = this, t = i.data.itemid, e = i.data.fromuid;
        app.util.request({
            url: "entry/wxapp/message",
            data: {
                act: "chat",
                fromuid: e,
                itemid: t,
                m: "superman_hand2"
            },
            success: function(t) {
                if (t.data.errno) i.showIconToast(t.errmsg); else {
                    var e = t.data.data, a = e.from.nickname;
                    wx.setNavigationBarTitle({
                        title: "与" + a + "聊天中"
                    });
                    for (var o = e.message, s = 0; s < o.length; s++) o[s].message = WxEmoji.explain(o[s].message);
                    i.setData({
                        info: e,
                        total: o.length,
                        msg_list: o
                    });
                }
            }
        });
    },
    initChat: function(a) {
        var o = this;
        app.util.request({
            url: "entry/wxapp/message",
            data: {
                act: "init",
                m: "superman_hand2"
            },
            success: function(t) {
                var e = t.data.data;
                o.ChatServer.init(a, e);
            }
        });
    },
    ChatServer: {
        init: function(e, a) {
            var o = this;
            wx.connectSocket({
                url: a.url,
                fail: function(t) {
                    console.log(t);
                }
            }), wx.onSocketOpen(function() {
                console.log("socket open");
                var t = {
                    type: "login",
                    sid: a.sid,
                    uid: e.memberInfo.uid,
                    nickname: e.memberInfo.nickname
                };
                o.send(t);
            }), wx.onSocketMessage(function(t) {
                o.receive(t);
            }), wx.onSocketError(function() {
                wx.showModal({
                    title: "提示",
                    content: "服务器连接失败，请稍后再试"
                });
            }), wx.onSocketClose(function() {
                console.log("WebSocket 已关闭！");
            });
        },
        send: function(t) {
            var e = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "", a = JSON.stringify(t);
            console.log(a), wx.sendSocketMessage({
                data: a,
                success: function() {
                    "function" == typeof e && e();
                }
            });
        },
        receive: function(t) {
            var e = getCurrentPages(), a = e[e.length - 1], o = JSON.parse(t.data);
            console.log(o);
            switch (o.type) {
              case "ping":
                this.send({
                    type: "pong"
                });
                break;

              case "say":
                a.getChatMsg();
            }
        },
        close: function() {
            wx.closeSocket(), wx.onSocketClose(function() {
                console.log("WebSocket 已关闭！");
            });
        }
    },
    showPopup: function() {
        this.setData({
            showBottomPopup: !0
        });
    },
    toggleBottomPopup: function() {
        this.setData({
            showBottomPopup: !this.data.showBottomPopup
        });
    },
    showEmoji: function() {
        var t = this;
        t.data.showWxEmojiView ? t.setData({
            showWxEmojiView: !1,
            bottom: 0,
            paddingBottom: "90rpx"
        }) : t.setData({
            showWxEmojiView: !0,
            bottom: "300rpx",
            paddingBottom: "400rpx"
        });
    },
    setValue: function(t) {
        this.setData({
            msg: t.detail.value
        });
    },
    wxPreEmojiTap: function(t) {
        var e = this, a = t.currentTarget.dataset.text, o = e.data.msg;
        o ? e.setData({
            msg: o + a,
            focus: !0
        }) : e.setData({
            msg: a,
            focus: !0
        });
    },
    sendMsg: function(t) {
        var e = this;
        e.data.showWxEmojiView && e.setData({
            bottom: 0,
            showWxEmojiView: !1
        });
        var a = e.data.fromuid, o = e.data.itemid, s = e.data.touid, i = e.data.msg;
        if ("" == i) return e.showIconToast("内容不能为空"), !1;
        if (2048 <= i.length) return e.showIconToast("消息太长，请分开发送"), !1;
        var n = {
            type: "say",
            from_uid: s,
            to_uid: a,
            itemid: o,
            content: i,
            form_id: t.detail.formId
        };
        e.ChatServer.send(n, e.getChatMsg()), e.setData({
            msg: "",
            paddingBottom: "90rpx"
        });
        var r = wx.getSystemInfoSync().windowHeight;
        wx.pageScrollTo({
            scrollTop: r + 60
        });
    },
    onPullDownRefresh: function() {
        var s = this;
        if (s.data.refresh) {
            if (s.data.total < 20) return s.setData({
                more: !1
            }), void wx.stopPullDownRefresh();
            s.setData({
                hide: !1
            });
            var i = s.data.pages + 1;
            app.util.request({
                url: "entry/wxapp/message",
                cachetime: "0",
                data: {
                    act: "chat",
                    page: i,
                    m: "superman_hand2"
                },
                success: function(t) {
                    if (s.setData({
                        hide: !0
                    }), wx.stopPullDownRefresh(), 0 == t.data.errno) {
                        var e = t.data.data.message;
                        if (0 < e.length) {
                            var a = s.data.msg_list;
                            s.setData({
                                total: e.length
                            });
                            var o = a.concat(e);
                            s.setData({
                                msg_list: o,
                                pages: i
                            });
                        } else s.setData({
                            more: !1,
                            refresh: !1
                        });
                    } else s.showIconToast(t.errmsg);
                }
            });
        } else wx.stopPullDownRefresh();
    },
    onUnload: function() {
        this.ChatServer.close();
    },
    showIconToast: function(t) {
        var e = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "fail";
        Toast({
            type: e,
            message: t,
            selector: "#zan-toast"
        });
    }
});